package org.example.project

import org.example.project.cache.Database
import org.example.project.cache.DatabaseDriverFactory
import org.example.project.entity.RocketLaunch
import org.example.project.log.LogLevel
import org.example.project.log.log
import org.example.project.network.SpaceXApi

class SpaceXSDK(databaseDriverFactory: DatabaseDriverFactory, val api : SpaceXApi) {
    private val database = Database(databaseDriverFactory)

    suspend fun getAllLaunches(forceReload: Boolean = false): List<RocketLaunch> {
        val cachedLaunches = database.getAllLaunches()
        if (cachedLaunches.isNotEmpty() && !forceReload) {
            return cachedLaunches
        }
        log(LogLevel.ERROR, "org.example.project", "getAllLaunches()....")
        return api.getAllLaunches().also {
            database.clearAndCreateLaunches(it)
        }
    }

}